<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

require dirname(__FILE__).'/pc_controller.php';

class Honda_validate extends PC_controller {

    public function __construct(){
        parent::__construct();
        $this->load->model('Usermodel','m_user');
        $this->load->model('Hondacustomermodel','m_customer');
        $this->load->model('configmodel','m_config');
    }

    public function index(){
        $data = parent::index();

        $this->load->view('admin/honda_validate',$data);
    }

    public function getCardData() {
        $this->validate->setRule('card_number','string');
        if(!$this->validate->run($this->input->post())){
            $this->response->setDetail($this->validate->getMessage());
            $this->response->responseExit();
        }
        $info = $this->validate->getResult();

        $card = $this->m_customer->getCardByNumber($info['card_number']);
        if(empty($card)) {
            $this->response->setDetail('卡券号错误！请检查!');
            $this->response->responseExit();
        }
        if($card['card_status'] == STATUS_ACTIVE) {
            $this->response->setDetail('卡券号已被激活！请确认!');
            $this->response->responseExit();
        }

        global $g_honda_card_type;
        $card['card_type_text'] = $g_honda_card_type[$card['card_type']];

        $this->response->setSuccess();
        $this->response->setAddon($card);
        $this->response->responseExit();
    }

    public function submitRecord() {
        $this->validate->setRule('card_number','string');
        $this->validate->setRule('record_name','string');
        $this->validate->setRule('record_phone','string');
        $this->validate->setRule('record_frame_number','string');
        $this->validate->setRule('record_models','string');
        if(!$this->validate->run($this->input->post())){
            $this->response->setDetail($this->validate->getMessage());
            $this->response->responseExit();
        }
        $info = $this->validate->getResult();
        if(!preg_match("/^1[0-9]\d{9}$/i", $info['record_phone'])){
            $this->response->setDetail('请输入正确的手机号码!');
            $this->response->responseExit();
        }

        $card = $this->m_customer->getCardByNumber($info['card_number']);
        if(empty($card)) {
            $this->response->setDetail('卡券号错误！请检查!');
            $this->response->responseExit();
        }
        if($card['card_status'] == STATUS_ACTIVE) {
            $this->response->setDetail('卡券号已被激活！请确认!');
            $this->response->responseExit();
        }

        $user = $this->session->userdata('user');
        if($card['card_type'] == 2) {
            if($card['customer_reservation_type'] == 1 && $user['shop_support_chezhu'] == 0) {
                $this->response->setDetail('该卡券需要支持车主讲堂！本店不支持车主讲堂!');
                $this->response->responseExit();
            }
            if($card['customer_reservation_type'] == 2 && $user['shop_support_shuangkuai'] == 0) {
                $this->response->setDetail('该卡券需要支持双快！本店不支持双快!');
                $this->response->responseExit();
            }
        }
        if($user['group_id'] != 2) {
            $this->response->setDetail('4s店才能核销!');
            $this->response->responseExit();
        }

        $this->m_customer->editCard(array(
            'card_id' => $card['card_id'],
            'card_status' => STATUS_ACTIVE,
            'card_time_active' => NOW,
        ));

        unset($info['card_number']);
        $info['user_id'] = $this->m_app->getCurrentUserId();
        $info['card_id'] = $card['card_id'];
        $info['record_time_create'] = NOW;
        $this->m_customer->createValRecord($info);

        $this->response->setSuccess();
        $this->response->setDetail($this->lang->line('success_update'));
        $this->response->responseExit();
    }

}
/* End of file feedback.php */
/* Location: ./application/controllers/feedback.php */